<?php

/**
 * This is the model class for table "tbl_admin".
 *
 * The followings are the available columns in table 'tbl_admin':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $address
 * @property string $phone
 * @property string $fax
 * @property string $name_display
 * @property string $website
 * @property string $description
 */


class Admin extends CActiveRecord
{
	// Properties for creating/updating password
	public $new_password;
	public $new_password_repeat;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Admin the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_admin';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		array('username, password, email,name_display, address, phone, fax, website, description', 'required'),
		array('username, password,email, phone, fax,name_display', 'length', 'max'=>60),
		array('website', 'length', 'max'=>150),
		// The following rule is used by search().
		// Please remove those attributes that should not be searched.
		array('id, username, password, email, address, phone, fax, website, description,name_display', 'safe', 'on'=>'search'),
		// Both passwords always must match, but are only required on create!
		array( 'new_password', 'length', 'max' => 60 ),
		array( 'new_password', 'compare' ),
		// Make new_password_repeat safe. If it doesn't appear in any other rule, it's considered not safe!
		array( 'new_password_repeat', 'safe' ),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'password' => 'Password',
			'email' => 'Email',
			'address' => 'Address',
			'phone' => 'Phone',
			'fax' => 'Fax',
			'website' => 'Website',
			'description' => 'Description',
			'name_display' =>'Name Display'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('fax',$this->fax,true);
		$criteria->compare('website',$this->website,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('name_display',$this->name_display,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
 // Password hashing stripped out in the sake of brevity :)
    protected function beforeSave()
    {
        // If new password is empty, it means it's an update with blank password,
        // so we leave it untouched. 
        // If it contains some value, then let's update the actual password
        // Remember that newPassword is required on create!
        if( ! empty( $this->new_password ) )
        {
            $this->password = $this->hashPassword($this->new_password);
        }
        return parent::beforeSave();
    }

	public function validatePassword($password)
	{	
		return $this->hashPassword($password)===$this->password;
	}

	public function hashPassword($password)
	{
		return md5($password);
	}

}